package com.automation.ljf.repository.impl.mapper;

import com.automation.ljf.domain.po.CaseStepPO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 操作mf_case_step表
 *
 * @author lijiefeng
 * @since 2021/8/25
 */
@Mapper
public interface StepMapper {

    /**
     * 根据用例id查询用例的所有执行步骤
     * @param caseId    用例id
     * @return  List<CaseStepPO>
     */
    @Select("<script>" +
            "select id, case_id, step_id, step_name, step_xpath, step_operation, step_relation, step_amount, step_jsonpath," +
            " is_input, expect_result, create_time, update_time" +
            " from mf_case_step where case_id = #{caseId}" +
            "<if test='tmp != null and tmp != \" \"'>" +
            "and is_input = #{tmp} " +
            "</if>" +
            "and is_delete = false " +
            "</script>")
    List<CaseStepPO> findCaseStepByCId(@Param("caseId") String caseId, @Param("tmp") Boolean isInput);


    @Select("select id, step_name, step_xpath, step_operation, step_relation, step_amount, step_jsonpath, " +
            "is_input, expect_result, create_time, update_time " +
            "from mf_case_step where case_id = #{caseId} and step_id = #{stepId} and is_delete = false")
    CaseStepPO findCaseStepByCIdAndStepId(@Param("caseId") String caseId, @Param("stepId") Integer stepId);


    /**
     * 添加用例步骤
     * @param caseStepPO   用例步骤PO实体
     * @return  Integer
     */
    @Insert({"<script>" +
            "insert into mf_case_step" +
            "(id, step_id, step_name, step_xpath, step_relation, step_operation, step_window, step_amount, step_jsonpath, expect_result, is_input, " +
            "case_id, create_time, update_time) " +
            "values" +
            "(#{cs.id}, #{cs.stepId}, #{cs.stepName}, #{cs.stepXpath}, #{cs.stepRelation}, #{cs.stepOperation}," +
            "#{cs.stepWindow}, #{cs.stepAmount}, #{cs.stepJsonpath}, #{cs.expectResult}, #{cs.isInput}," +
            "#{cs.caseId}, #{cs.createTime}, #{cs.updateTime})" +
            "</script>"})
    Integer insertCaseStep(@Param("cs") CaseStepPO caseStepPO);

//    @Update("<script>" +
//            "update mf_case_step set is_delete = true " +
//            "<foreach collection='list' item='item' index='index' separator=','>" +
//            "where case_id = #{item.caseId} and step_id = #{item.stepId}" +
//            "</foreach>" +
//            "</script>")
    @Delete("<script>" +
            "delete from mf_case_step " +
            "<if test='list != null'>" +
            "<foreach collection='list' item='item' index='index' separator=','>" +
            "where case_id = #{caseId} " +
            "<if test='#{item.stepId} != null'>" +
            "and step_id = #{item.stepId}" +
            "</if>" +
            "</foreach>" +
            "</if>" +
            "<if test='list == null'>" +
            "where case_id = #{caseId}" +
            "</if>" +
            "</script>")
    Integer deleteStepByCaseId(@Param("caseId") String caseId,
                               @Param("list") List<CaseStepPO> caseStepPOS);

}
